<?php
require dirname(__FILE__) . DIRECTORY_SEPARATOR . 'autoload.php';

use payClass\Datebase;
use payClass\QRcode;
use payClass\pay\Config;

/**
 * 扫码支付
 */
class Scan
{
	public static function scanDo()
	{
		$db     = new Datebase();

		$config = new Config();

		$param  = $db->param();

		// 订单号
		$param['orderNumber'] = 'C' . time() . 'U' . $param['uid'];

		$param['paytype'] = Config::$folder.'-'.Config::$merchant;
		// 数据检测
		// $verifyRes = $db->verifyScan($param);

		// if ($verifyRes != 'Y') $db->ajaxReturn($verifyRes);

		$data = [
			'merchantId'    => (string)Config::$merchant,
			'timestamp'     => (string)Config::getMsectime(),
			'userName'      => (string)time(),
			'userIp'        => '223.104.' . rand(0,255) . '.' . rand(1,255),
			'thirdOrderId'  => $param['orderNumber'],
			'amount'        => (double)sprintf("%.2f", $param['price']),
			'payWayChannel' => $param['scanType'],
			'currency'      => 'CNY',
			'notifyUrl'     => Config::$notifyUrl,
		];

		$data['sign'] = Config::getSign($data);
		// var_dump($data);
		$curlRes = Config::setHttpRequest(Config::$api, $data, 'POST', ['Content-Type:application/x-www-form-urlencoded; charset=UTF-8']);

		$resArray = json_decode($curlRes, true);

		if ($resArray['code'] != '0') $db->ajaxReturn($resArray['message']);

		$db->ajaxReturn($resArray['payUrl']);
	}
}

Scan::scanDo();
